/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ifs.RnD.Graph;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;

/**
 *
 * @author chjslk
 */
public class Favourite {

    public String favouriteURLs;
    public String fileName, userHome;
    String readings;
    public String words;
    public ArrayList<String> favURLs;
    String over[];
    boolean lineExist;
    boolean conditn;

    /*
     * Creates a new instance of Favourite
     */
    public Favourite() {
        userHome = null;
        userHome = getUserHome();
        fileName = userHome + "\\My Documents\\VisioSVN\\Favourite.txt";
        words = "";
        favURLs = new ArrayList<String>();
        lineExist = false;
        conditn = false;
        makeDir();

    }

    /** this method calls the method to write the favourite URL in the txt file
     * @param fav
     */
    public void save(String fav) {
        favouriteURLs = fav;
        writeFile();
    }
    /*
     * This writes the URL in the txt file
     */

    public void writeFile() {
        try {
            // Create file 
            readFileForWrite();
            if (!checkURL(favouriteURLs)) {
                FileWriter fstream = new FileWriter(fileName, true);
                BufferedWriter writer = new BufferedWriter(fstream);
                PrintWriter out = new PrintWriter(writer);
                out.println(favouriteURLs);
                //Close the output stream
                out.close();
            }
            System.out.println(favURLs);
        } catch (Exception e) {//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
    }

    /** Creates a new folder or check the existence of the folder 
     * @param s
     *  @param e
     */
    public void makeDir() {
        File f = new File(userHome + "\\My Documents\\VisioSVN");
        try {
            if (f.mkdir()) {
                System.out.println("Directory Created");
            } else {
                System.out.println("Directory is not created");
            }
        } catch (Exception e) {
        }
        try {
            if (!f.exists()) {
                f.createNewFile();
            }
        } catch (Exception e) {
        }
    }
    /*
     * reads the file before write for check whether the URL already writtn.
     */

    public void readFileForWrite() {
        favURLs.clear();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
            readings = reader.readLine();
            while (readings != null) {
                words = words + readings + " ";
                System.out.println("read    :" + readings);
                favURLs.add(readings);
                readings = reader.readLine();
            }
            reader.close();
        } catch (Exception e) {
            System.out.println("No URL to read");
        }
    }
    /*
     * reads the file for set URLs in the combo box
     */

    public ArrayList readFileForView(ArrayList viewURLs) {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
            readings = reader.readLine();
            while (readings != null) {
                words = words + readings + " ";
                System.out.println("read    :" + readings);
                viewURLs.add(readings);
                readings = reader.readLine();
            }
            reader.close();
        } catch (Exception e) {
            System.out.println("No URL to view");
        }
        return viewURLs;
    }
    /*
     * reads the file for set the URLs in the main fram.
     */

    public ArrayList readFileForMain(ArrayList displayURLs) {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
            readings = reader.readLine();
            while (readings != null) {
                words = words + readings + " ";
                System.out.println("read    :" + readings);
                displayURLs.add(readings);
                readings = reader.readLine();
            }
            reader.close();
        } catch (Exception e) {
            System.out.println("No URL to set in main frame");
        }
        return displayURLs;
    }
    /*
     * Checks the existence of the URL.
     */

    public boolean checkURL(String url) {
        for (int i = 0; i < favURLs.size(); i++) {
            if (url.equals(favURLs.get(i))) {
                conditn = true;
            }

        }
        favURLs.clear();
        lineExist = conditn;
        return lineExist;
    }
    /*
     * Identify the Users' home ditory
     */

    public String getUserHome() {
        return System.getProperty("user.home");
    }
}
